package nl.hanze.designpatterns.DAO.impl.file.crypted;

import java.io.FileInputStream;

import nl.hanze.designpatterns.DAO.LoginCredentialDAO;
import nl.hanze.designpatterns.domain.LoginCredential;

public class LoginCredentialsDAOImplFileCrypted implements LoginCredentialDAO{

	@Override
	public boolean isValid(LoginCredential logincredential) {
		try {
			FileInputStream fis = new FileInputStream("login2.ser");
			CryptedInputStream cis = new CryptedInputStream(fis);
			LoginCredential lc = (LoginCredential) cis.getObject();
			byte[] username = lc.getUserName().getBytes();
			lc.setUserName(cis.decrypt(username, CryptedInputStream.encryptionKey));
			byte[] password = lc.getPassword().getBytes();
			lc.setPassword(cis.decrypt(password, CryptedInputStream.encryptionKey));
			
			//check the password and username
			//return true if valid
			if((lc.getUserName()==null)&&(lc.getPassword()==null)){
				return false;
			}
			else if((lc.getUserName().equals(logincredential.getUserName()))&&(lc.getPassword().equals(logincredential.getPassword()))){
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public LoginCredential getCredential(String username) throws Exception {
		
		return null;
	}

	
}
